home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / machine / galaga.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  5KB  |  261 lines

  1. /***************************************************************************
  2.  
  3.   machine.c
  4.  
  5.   Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
  6.   I/O ports)
  7.  
  8. ***************************************************************************/
  9.  
  10. #include "driver.h"
  11. #include "vidhrdw/generic.h"
  12. #include "cpu/z80/z80.h"
  13.  
  14.  
  15. unsigned char *galaga_sharedram;
  16. static unsigned char interrupt_enable_1,interrupt_enable_2,interrupt_enable_3;
  17.  
  18. static void *nmi_timer;
  19.  
  20. WRITE_HANDLER( galaga_halt_w );
  21. void galaga_vh_interrupt(void);
  22.  
  23.  
  24. void galaga_init_machine(void)
  25. {
  26.     nmi_timer = 0;
  27.     galaga_halt_w (0, 0);
  28. }
  29.  
  30.  
  31.  
  32. READ_HANDLER( galaga_sharedram_r )
  33. {
  34.     return galaga_sharedram[offset];
  35. }
  36.  
  37.  
  38.  
  39. WRITE_HANDLER( galaga_sharedram_w )
  40. {
  41.     if (offset < 0x800)        /* write to video RAM */
  42.         dirtybuffer[offset & 0x3ff] = 1;
  43.  
  44.     galaga_sharedram[offset] = data;
  45. }
  46.  
  47.  
  48.  
  49. READ_HANDLER( galaga_dsw_r )
  50. {
  51.     int bit0,bit1;
  52.  
  53.  
  54.     bit0 = (input_port_0_r(0) >> offset) & 1;
  55.     bit1 = (input_port_1_r(0) >> offset) & 1;
  56.  
  57.     return bit0 | (bit1 << 1);
  58. }
  59.  
  60.  
  61.  
  62. /***************************************************************************
  63.  
  64.  Emulate the custom IO chip.
  65.  
  66. ***************************************************************************/
  67. static int customio_command;
  68. static int mode,credits;
  69. static int coinpercred,credpercoin;
  70. static unsigned char customio[16];
  71.  
  72.  
  73. WRITE_HANDLER( galaga_customio_data_w )
  74. {
  75.     customio[offset] = data;
  76.  
  77. logerror("%04x: custom IO offset %02x data %02x\n",cpu_get_pc(),offset,data);
  78.  
  79.     switch (customio_command)
  80.     {
  81.         case 0xa8:
  82.             if (offset == 3 && data == 0x20)    /* total hack */
  83.                 sample_start(0,0,0);
  84.             break;
  85.  
  86.         case 0xe1:
  87.             if (offset == 7)
  88.             {
  89.                 coinpercred = customio[1];
  90.                 credpercoin = customio[2];
  91.             }
  92.             break;
  93.     }
  94. }
  95.  
  96.  
  97. READ_HANDLER( galaga_customio_data_r )
  98. {
  99.     if (customio_command != 0x71)
  100.         logerror("%04x: custom IO read offset %02x\n",cpu_get_pc(),offset);
  101.  
  102.     switch (customio_command)
  103.     {
  104.         case 0x71:    /* read input */
  105.         case 0xb1:    /* only issued after 0xe1 (go into credit mode) */
  106.             if (offset == 0)
  107.             {
  108.                 if (mode)    /* switch mode */
  109.                 {
  110.                     /* bit 7 is the service switch */
  111.                     return readinputport(4);
  112.                 }
  113.                 else    /* credits mode: return number of credits in BCD format */
  114.                 {
  115.                     int in;
  116.                     static int coininserted;
  117.  
  118.  
  119.                     in = readinputport(4);
  120.  
  121.                     /* check if the user inserted a coin */
  122.                     if (coinpercred > 0)
  123.                     {
  124.                         if ((in & 0x70) != 0x70 && credits < 99)
  125.                         {
  126.                             coininserted++;
  127.                             if (coininserted >= coinpercred)
  128.                             {
  129.                                 credits += credpercoin;
  130.                                 coininserted = 0;
  131.                             }
  132.                         }
  133.                     }
  134.                     else credits = 2;
  135.  
  136.  
  137.                     /* check for 1 player start button */
  138.                     if ((in & 0x04) == 0)
  139.                         if (credits >= 1) credits--;
  140.  
  141.                     /* check for 2 players start button */
  142.                     if ((in & 0x08) == 0)
  143.                         if (credits >= 2) credits -= 2;
  144.  
  145.                     return (credits / 10) * 16 + credits % 10;
  146.                 }
  147.             }
  148.             else if (offset == 1)
  149.                 return readinputport(2);    /* player 1 input */
  150.             else if (offset == 2)
  151.                 return readinputport(3);    /* player 2 input */
  152.  
  153.             break;
  154.     }
  155.  
  156.     return -1;
  157. }
  158.  
  159.  
  160. READ_HANDLER( galaga_customio_r )
  161. {
  162.     return customio_command;
  163. }
  164.  
  165.  
  166. void galaga_nmi_generate (int param)
  167. {
  168.     cpu_cause_interrupt (0, Z80_NMI_INT);
  169. }
  170.  
  171.  
  172. WRITE_HANDLER( galaga_customio_w )
  173. {
  174.     if (data != 0x10 && data != 0x71)
  175.         logerror("%04x: custom IO command %02x\n",cpu_get_pc(),data);
  176.  
  177.     customio_command = data;
  178.  
  179.     switch (data)
  180.     {
  181.         case 0x10:
  182.             if (nmi_timer) timer_remove (nmi_timer);
  183.             nmi_timer = 0;
  184.             return;
  185.  
  186.         case 0xa1:    /* go into switch mode */
  187.             mode = 1;
  188.             break;
  189.  
  190.         case 0xe1:    /* go into credit mode */
  191.             credits = 0;    /* this is a good time to reset the credits counter */
  192.             mode = 0;
  193.             break;
  194.     }
  195.  
  196.     nmi_timer = timer_pulse (TIME_IN_USEC (50), 0, galaga_nmi_generate);
  197. }
  198.  
  199.  
  200.  
  201. WRITE_HANDLER( galaga_halt_w )
  202. {
  203.     if (data & 1)
  204.     {
  205.         cpu_set_reset_line(1,CLEAR_LINE);
  206.         cpu_set_reset_line(2,CLEAR_LINE);
  207.     }
  208.     else if (!data)
  209.     {
  210.         cpu_set_reset_line(1,ASSERT_LINE);
  211.         cpu_set_reset_line(2,ASSERT_LINE);
  212.     }
  213. }
  214.  
  215.  
  216.  
  217. WRITE_HANDLER( galaga_interrupt_enable_1_w )
  218. {
  219.     interrupt_enable_1 = data & 1;
  220. }
  221.  
  222.  
  223.  
  224. int galaga_interrupt_1(void)
  225. {
  226.     galaga_vh_interrupt();    /* update the background stars position */
  227.  
  228.     if (interrupt_enable_1) return interrupt();
  229.     else return ignore_interrupt();
  230. }
  231.  
  232.  
  233.  
  234. WRITE_HANDLER( galaga_interrupt_enable_2_w )
  235. {
  236.     interrupt_enable_2 = data & 1;
  237. }
  238.  
  239.  
  240.  
  241. int galaga_interrupt_2(void)
  242. {
  243.     if (interrupt_enable_2) return interrupt();
  244.     else return ignore_interrupt();
  245. }
  246.  
  247.  
  248.  
  249. WRITE_HANDLER( galaga_interrupt_enable_3_w )
  250. {
  251.     interrupt_enable_3 = !(data & 1);
  252. }
  253.  
  254.  
  255.  
  256. int galaga_interrupt_3(void)
  257. {
  258.     if (interrupt_enable_3) return nmi_interrupt();
  259.     else return ignore_interrupt();
  260. }
  261.